﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using Maticsoft.Common;
using LTP.Accounts.Bus;
namespace lcs.Web.spaceinfo
{
    public partial class Modify : Web.Admin.BasePage
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                lcs.BLL.sptype bs = new BLL.sptype();
                ddltype.DataSource = bs.GetAllList().Tables[0];
                ddltype.DataTextField = "sptname";
                ddltype.DataValueField = "sptid";
                ddltype.DataBind();
                StringBuilder strWhere = new StringBuilder();
                lcs.BLL.storeinfo bst = new BLL.storeinfo();

                if (Session["login"] != null)
                {
                    DataSet u = (DataSet)Session["login"];

                    if (u.Tables[0].Rows[0]["roles_id"].ToString() == "2")
                    {
                        strWhere.AppendFormat(" uid={0}", u.Tables[0].Rows[0]["user_id"]);

                        ddlstore.DataSource = bst.GetddlList(strWhere.ToString());
                        ddlstore.DataTextField = "sname";
                        ddlstore.DataValueField = "sid";
                        ddlstore.DataBind();
                    }
                }
                if (Request.Params["id"] != null && Request.Params["id"].Trim() != "")
                {
                    int spid = (Convert.ToInt32(Request.Params["id"]));
                    ShowInfo(spid);
                }
            }
        }

        private void ShowInfo(int spid)
        {
            lcs.BLL.spaceinfo bll = new lcs.BLL.spaceinfo();
            lcs.Model.spaceinfo model = bll.GetModel(spid);
            //this.lblspid.Text = model.spid.ToString();
            this.txtspname.Text = model.spname;
            this.ddltype.SelectedValue = model.sptype.ToString();
            this.txtspadd.Text = model.spadd;
            this.ddlstore.SelectedValue = model.sid.ToString();
            this.ddlstatus.SelectedValue = model.spstatus.ToString();
            this.txtspdes.Text = model.spdes;
            this.txtspremark.Text = model.spremark;

        }

        public void btnSave_Click(object sender, EventArgs e)
        {

            string strErr = "";
            if (this.txtspname.Text.Trim().Length == 0)
            {
                strErr += "仓位名称不能为空！\\n";
            }

            if (this.txtspadd.Text.Trim().Length == 0)
            {
                strErr += "仓位地址不能为空！\\n";
            }

            if (this.txtspdes.Text.Trim().Length == 0)
            {
                strErr += "仓位描述不能为空！\\n";
            }
            if (this.txtspremark.Text.Trim().Length == 0)
            {
                strErr += "仓位备注不能为空！\\n";
            }
            if (string.IsNullOrEmpty(this.ddlstore.SelectedValue))
            {
                strErr += "仓库不能为空！\\n";
            }
           

            if (strErr != "")
            {
                MessageBox.Show(this, strErr);
                return;
            }
            int spid = 0;
            if (Request.Params["id"] != null && Request.Params["id"].Trim() != "")
            {
                spid = (Convert.ToInt32(Request.Params["id"]));                
            }
            string spname = this.txtspname.Text;
            int sptype = int.Parse(this.ddltype.SelectedValue.ToString());
            string spadd = this.txtspadd.Text;
            int sid = int.Parse(this.ddlstore.SelectedValue.ToString());
            int spstatus = int.Parse(this.ddlstatus.SelectedValue.ToString());
            string spdes = this.txtspdes.Text;
            string spremark = this.txtspremark.Text;


            lcs.Model.spaceinfo model = new lcs.Model.spaceinfo();
            model.spid = spid;
            model.spname = spname;
            model.sptype = sptype;
            model.spadd = spadd;
            model.sid = sid;
            model.spstatus = spstatus;
            model.spdes = spdes;
            model.spremark = spremark;

            lcs.BLL.spaceinfo bll = new lcs.BLL.spaceinfo();
            bll.Update(model);
            Maticsoft.Common.MessageBox.ShowAndRedirect(this, "保存成功！", "List.aspx");

        }


        public void btnCancle_Click(object sender, EventArgs e)
        {
            Response.Redirect("List.aspx");
        }
    }
}
